<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="110px">
      <el-form-item label="发货单号" prop="fahuoNo">
        <el-input v-model="queryParams.fahuoNo" placeholder="请输入发货单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单编号" prop="proNo">
        <el-input v-model="queryParams.proNo" placeholder="请输入订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入产品编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="models">
        <el-input v-model="queryParams.models" placeholder="请输入产品型号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight" @sort-change="handleSortChange" :summary-method="getSummaries"
              show-summary>
      <el-table-column type="index" label="序号" width="60" align="center"/>
      <el-table-column label="订单编号" align="center" prop="proNo" show-overflow-tooltip/>
      <el-table-column label="发货单号" align="center" prop="fahuoNo" show-overflow-tooltip/>
      <el-table-column label="产品编号" align="center" prop="nos" show-overflow-tooltip/>
      <el-table-column label="产品名称" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="产品型号" align="center" prop="models" sortable show-overflow-tooltip/>
      <el-table-column label="单位" align="center" prop="danwei"/>
      <el-table-column label="产品单价" align="center" prop="danjian"/>
      <el-table-column sortable label="发货状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yifahuo_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="发货日期" align="center" prop="fahuoDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fahuoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="交货日期" align="center" prop="jiaohuoDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jiaohuoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="发货数量" align="center" prop="shuliang"/>
      <el-table-column sortable label="发货金额" align="center" prop="money"/>


    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </el-dialog>
</template>

<script>
import {deleteAction, getAction, accAdd} from '@/api/manage'
export default {
  dicts: ['yifahuo_status'],
  name: "LvYifahuomingxiDetailList",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货单明细表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tongzhiId: null,
        tongzhiNo: null,
        jiaohuoDate: null,
        caigouNo: null,
        gonghuoNo: null,
        proId: null,
        proNo: null,
        prodName: null,
        saleId: null,
        name: null,
        models: null,
        mingxi: null,
        nos: null,
        danwei: null,
        danjian: null,
        shuliang: null,
        money: null,
        status: null,
        fahuoId: null,
        fahuoNo: null,
        orderByColumn: "fahuoNo",
        isAsc: "desc"
      },
      url: {
        list: '/lvYifahuomingxi/lvYifahuomingxi/list',
        remove: '/lvYifahuomingxi/lvYifahuomingxi/',
        export: '/lvYifahuomingxi/lvYifahuomingxi/export',
      },
      tableMaxHeight: window.innerHeight - 320,
      selectedRows: [], // 用于存储勾选的行数据
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询发货单明细列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    handleShowBy(lvYifahuoId) {
      this.title = "发货明细";
      this.queryParams.fahuoId = lvYifahuoId;
      this.open = true;
      this.getList();
    },


    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "money" || column.property === "shuliang") {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },


  }
};
</script>
